﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Editor
{
    public class EditorCursor
    {
        private int _HEIGHT = 32;
        private int _WIDTH = 32;
        private int _OFFSET = 0;
        public int OldX = 1;
        public int OldY = 1;
        private Pen _drawPen = new Pen(Color.Red, 2.0f);
        private Pen _clearPen = new Pen(Color.Transparent, 1.0f);
        private Graphics _cursor1GraphicsDevice;
        private Bitmap _backgroundGraphics;
        private Bitmap _CursorGraphics;
        private Graphics _DrawingGraphics;
        private Point _cursorPoint = new Point();
        public Point backgroundLocation = new Point(0, 0);
        private delegate void nullDelegate();
        private Rectangle _drawingBounds;
        private nullDelegate RenderBackground;
        private Rectangle _backgroundRectangle;
        public int OFFSET
        {
            get { return _OFFSET; }
            set { _OFFSET = OFFSET; }
        }
        public int HEIGHT
        {
            get { return _HEIGHT; }
            set { _HEIGHT = value; }
        }
        public int WIDTH
        {
            get { return _WIDTH; }
            set { _WIDTH = value; }
        }

        public EditorCursor(ref Graphics cursor1GraphicsDevice, ref Bitmap BackgroundGraphics)
        {
            _DrawingGraphics = cursor1GraphicsDevice;
            _CursorGraphics = new Bitmap(BackgroundGraphics.Width, BackgroundGraphics.Height);
             _cursor1GraphicsDevice = Graphics.FromImage(_CursorGraphics);
            _backgroundGraphics = BackgroundGraphics;
            _cursor1GraphicsDevice.Clear(Color.Transparent);
            _drawingBounds = Rectangle.Truncate(_DrawingGraphics.VisibleClipBounds);
            UpdatePosition(0, 0);
        }

        public void UpdatePosition(int X, int Y)
        {
            if (X != OldX || Y != OldY)
            {
                _cursorPoint.X = (X* _WIDTH);
                _cursorPoint.Y = Y * _HEIGHT;
                OldX = X;
                OldY = Y;
                DrawRectangle1();
            }

        }
        public void AssignNewBitmap(IDisposable image)
        {
            _backgroundGraphics = (Bitmap)image;
        }
        public void DrawRectangle1(Bitmap image)
        {
            _cursor1GraphicsDevice.Clear(Color.Transparent);
            _cursor1GraphicsDevice.DrawRectangle(_drawPen, new Rectangle(_cursorPoint, new Size(HEIGHT, WIDTH)));
            _DrawingGraphics.DrawImageUnscaled(image, new Point(0, 0));
            _DrawingGraphics.DrawImageUnscaled(_CursorGraphics, new Point(0, 0));
        }
        public void DrawRectangle1()
        {
           // _DrawingGraphics.Clear(Color.Black);
            _cursor1GraphicsDevice.Clear(Color.Transparent);
            _cursor1GraphicsDevice.DrawRectangle(_drawPen,new Rectangle(_cursorPoint,new Size(HEIGHT,WIDTH)));
            _DrawingGraphics.DrawImage(_backgroundGraphics, _drawingBounds, new Rectangle(backgroundLocation, _drawingBounds.Size), GraphicsUnit.Pixel);
            _DrawingGraphics.DrawImageUnscaled(_CursorGraphics, new Point(0, 0));
        }
        private void drawBackgroundMoving()
        {
            _DrawingGraphics.DrawImage(_backgroundGraphics, _drawingBounds, new Rectangle(backgroundLocation, _drawingBounds.Size), GraphicsUnit.Pixel);
        }
        private void drawBackgroundStandard()
        {
            _DrawingGraphics.DrawImageUnscaled(_backgroundGraphics, backgroundLocation);
        }

    }
}
